<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\App\WorkContact\Action\Dashboard;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use MoChat\App\Common\Middleware\DashboardAuthMiddleware;
use MoChat\App\User\Logic\Traits\UserTrait;
use MoChat\App\WorkContact\Contract\WorkContactContract;
use MoChat\Framework\Action\AbstractAction;
use MoChat\Framework\Request\ValidateSceneTrait;

/**
 * 组织管理- 查看人员列表.
 *
 * Class GetDepartmentEmployee.
 * @Controller
 */
class GetContact extends AbstractAction
{
    use ValidateSceneTrait;
    use UserTrait;

    /**
     * @Inject
     * @var WorkContactContract
     */
    protected $workContactService;

    /**
     * @Middlewares({
     *     @Middleware(DashboardAuthMiddleware::class)
     * })
     * @RequestMapping(path="/dashboard/workContact/getContact", methods="GET")
     * @return array 返回数组
     */
    public function handle(): array
    {
        // 参数验证
        $this->validated($this->request->all());

        $corpId = $this->corpId();
        $params = [
            'page' => $this->request->input('page', '1'),
            'perPage' => $this->request->input('perPage', '10'),
        ];

        $where = [
            ['corp_id', '=', $corpId],
        ];
        if (! empty($this->request->query('contactName'))) {
            $where[] = ['name', 'like', '%' . $this->request->query('contactName') . '%'];
        }

        $toData = $this->workContactService->getWorkContactList($where, ['id', 'name', 'avatar', 'wx_external_userid'], $params);

        return [
            'page' => [
                'perPage' => $toData['per_page'],
                'total' => $toData['total'],
                'totalPage' => $toData['last_page'],
            ],
            'list' => $toData['data'],
        ];
    }

    /**
     * 验证规则.
     *
     * @return array 响应数据
     */
    protected function rules(): array
    {
        return [
        ];
    }

    /**
     * 验证错误提示.
     * @return array 响应数据
     */
    protected function messages(): array
    {
        return [
        ];
    }
}
